import path from 'path';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { viteExternalsPlugin as externals } from 'vite-plugin-externals';
import inject from '@rollup/plugin-inject';

export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    externals({ // 将element-plus用cdn的形式导入，key为包名，value为全局变量的名称
      'vue': 'Vue',
      'vue-router': 'VueRouter',
      'element-plus': 'ElementPlus',
    }),
    inject({
      'utils': [path.resolve('src/utils/index.js'), '*'],
      'lc': path.resolve('src/utils/lc.js'),
      '_': path.resolve('src/utils/lodash.js'),
      'api': path.resolve('src/api/index.js'),
      'CO': path.resolve('src/constants/index.js'),
      'store': [path.resolve('src/store/index.js'), '*'],
      'useRouter': path.resolve('src/config/useRouter.js'),
      '$alert': path.resolve('src/config/$alert.js'),
      '$confirm': path.resolve('src/config/$confirm.js'),
      '$prompt': path.resolve('src/config/$prompt.js'),
      '$success': path.resolve('src/config/$success.js'),
      '$error': path.resolve('src/config/$error.js'),
    }),
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'),
    },
    extensions: ['.vue', '.js'],
  },
  server: {
    host: '127.0.0.1', // 服务器
    port: 3000, // 端口
    proxy: {
      '/api': { // 工程的接口以/api开头
        target: 'http://localhost:5188', // 被代理服务
        changeOrigin: true, // 改变源
        rewrite: (path) => { // 映射规则
          return path.replace(/^\/api/, ''); // 将 http://localhost:3000/api/list/region 映射到http://localhost:5188/list/region
        },
      },
      '/uploadFile': { // 工程的接口以/api开头
        target: 'http://localhost:5188', // 被代理服务
        changeOrigin: true, // 改变源
      },
    },
  },
})
